<template>
  <div class="app-container">
    <!-- ****************************** -->
    <el-row :gutter="20" class="header" style="margin-left: 0;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="适用车型" prop="checklistType">
          <el-select filterable v-model="queryParams.checklistType" placeholder="所有项" clearable
            @change="currentCarType">
            <el-option v-for="item in carData" :key="item.id" :label="item.applicableModels"
              :value="item.type" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="车牌号" prop="wagonNumber">
          <el-input v-model="queryParams.wagonNumber" placeholder="车牌号"> </el-input>
        </el-form-item> -->
        <el-form-item label="年份" prop="year">
          <el-date-picker v-model="queryParams.year" style="width: 240px" type="year"
            value-format="yyyy"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- ****************************** -->
    <!-- 菜单列表 -->
    <el-table v-loading="loading" :data="menuList" style="width: 100%">
      <el-table-column prop="applicableModels" label="适用车型" align="center"></el-table-column>
      <el-table-column prop="wagonNumber" label="车号" align="center"></el-table-column>
      <el-table-column v-for="(item, index) in col" :key="index" :label="item" align="center">
        <template slot-scope="scope">{{ scope.row.fuelChargeList[index].refuelingVolume }}</template>
      </el-table-column>
      <el-table-column prop="tot" label="单车年合计">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/system/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { checkList, getCarData, changeRefuelingVolume, annualStatistics } from "@/api/system/check"
export default {
  name: "Menu",
  dicts: ['sys_show_hide', 'sys_normal_disable'],
  components: { Treeselect, IconSelect },
  data () {
    return {
      col: ['—月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月',],
      /* 日期 start*/
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 日期范围
      dateRange: [],
      /* 日期 end*/
      //车辆信息
      carData: [],
      //当前车型的车号
      currtentCarNumber: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checklistType: "",
        wagonNumber: "",
        year: this.$moment().add(-7, 'days').format('YYYY'),
      },
      // 表单参数
      form: {},
    };
  },
  created () {
    this.getList();
    this.getCarData()
  },
  activated () {
    this.getList();
    this.getCarData()
  },

  methods: {
    // 选择图标
    selected (name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList () {
      this.loading = true;
      /*start 请求数据转换*/
      this.queryParams.startTime = this.dateRange[0]
      this.queryParams.endTime = this.dateRange[1]
      annualStatistics(this.queryParams).then(response => {
        for(let i of response.data){
          let a = 0;
          for(let j of i.fuelChargeList){
            a += +j.refuelingVolume
          }
          i.tot = a
        }
    
        this.menuList = response.data;

        this.loading = false;
      });
    },
    /** 查询车辆数据 */
    getCarData () {
      getCarData().then(response => {
        this.carData = response.data
      });
    },
    //下拉框更新的车号
    currentCarType () {
      // this.queryParams.wagonNumber = ''
      // this.currtentCarNumber = this.carData.filter(item => {
      //   return item.applicableModels == this.queryParams.checklistType
      // })[0].applicableModelsData
    },
    /** 转换菜单数据结构 */
    normalizer (node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect () {
      listMenu().then(response => {
        this.menuOptions = [];
        const menu = { menuId: 0, menuName: '主类目', children: [] };
        menu.children = this.handleTree(response.data, "menuId");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$modal.confirm('是否确认删除名称为"' + row.menuName + '"的数据项？').then(function () {
        return delMenu(row.menuId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 编辑补加油量
    putRefuelingVolume (row) {
      this.$prompt(`请输入车号为 ${row.wagonNumber} 的补加油量`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: row.refuelingVolume,
        inputPattern: /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*)|0)$/,
        inputErrorMessage: '请输入数字(支持两位小数)'
      }).then(({ value }) => {
        console.log(value, row)
        let data = {}
        data.id = row.id
        data.refuelingVolume = value
        changeRefuelingVolume(data).then(() => {
          //刷新菜单
          this.getList();
          this.$message({
            type: 'success',
            message: '修改成功'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
      // this.edit = !this.edit
    }
  },
};
</script>
<style></style>